
from dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv())

# from openai import OpenAI
# client = OpenAI()
# model = "gpt-4o"
from zhipuai import ZhipuAI
client = ZhipuAI()
model = "glm-4v-plus"

def get_completion(messages, model="glm-4v-plus", temperature=0.01):
    response = client.chat.completions.create(
        model=model,
        messages=messages,
        temperature=temperature  
    )
    return response.choices[0].message.content


while True:
    image_path = input("请输入图片路径（输入'exit'退出程序）: ") 
    if image_path is None or image_path == "" :
        continue
    elif image_path.lower() == 'exit':  
        print("程序退出。\n")  
        exit
    else:
        break
    
import base64
with open(image_path, 'rb') as img_file:
    img_base = base64.b64encode(img_file.read()).decode('utf-8')

while True:
    user_input = input("请输入提示文字（输入'exit'退出程序）: ")  
    if user_input is None or user_input == "" :
        continue
    elif user_input.lower() == 'exit':  
        print("程序退出。")  
        break  
    else :
        messages=[
          {
            "role": "user",
            "content": [
              {
                "type": "image_url",
                "image_url": {
                    # "url" : f"data:image/jpeg;base64,{img_base}"
                    "url" : img_base
                }
              },
              {
                "type": "text",
                "text": f"{user_input}"
              }
            ]
          },
        ]
        break

res = get_completion(messages=messages, model=model, temperature=0.7)  
print("===大语言模型completion：===") 
print(res)
print("============END============\n")  

messages.append(
    {
      "role": "assistant",
      "content": f"{res}"
    }
)
  
while True:  
    user_input = input("请输入提示文字（输入'exit'退出程序）: ")  
    if user_input is None or user_input == "" :
        continue
    elif user_input.lower() == 'exit':  
        print("程序退出。")  
        break  
    else :
        messages.append(
          {
            "role": "user",
            "content": f"{user_input}"
          }
        )
        res = get_completion(messages=messages, model=model, temperature=0.7)  
        messages.append(
          {
            "role": "assistant",
            "content": f"{res}"
          }
        ) 
        print("===大语言模型completion：===") 
        print(res)
        print("============END============\n")